#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#define MAXN 1005
#define MAXM 100005
using namespace std;

int C[MAXN];
int uf[MAXN];

int find(int x){
    if (x == uf[x]) return x;
    return uf[x] = find(uf[x]);
}

void union1(int x, int y){
    uf[find(x)] = find(y);
}

int main()
{
    int n,m;
    freopen("truth.txt","r", stdin);
    scanf("%d", &n);
    scanf("%d", &m);

    for (int i=0;i<n;i++) uf[i] = i;
    for (int i=0;i<n;i++) C[i] = i+1;

    for (int t=0;t<m;t++){
        int x,y;
        char c;
        scanf("%d %d %c\n", &x, &y, &c);
        x--; y--;
        //printf("%d%d%c\n", x, y, c);
        union1(x,y);
        bool T = c == 'T';
        if ((C[x] == C[y] && !T) || (C[x]==-C[y] && T)){
            printf("%d\n", t);
            return 0;
        }
        int cy = C[y];
        int cx = T ? C[x] : -C[x];
        for (int i=0;i<n;i++){
            if (C[i] == cy) C[i] = cx;
            else if (C[i] == -cy) C[i] = -cx;
        }
    }
    printf("%d\n", m);
    return 0;
}
